package controller;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import pojo.Order;
import service.OrderService;

import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private Result result;

    /**
     * 查询条件
     */
    private Order condition;


    /**
     * 分页显示数据处理器
     *
     * @param mv
     * @param pageNum
     * @return
     */
    @RequestMapping("/orderList")
    public ModelAndView orderList(ModelAndView mv, @RequestParam(defaultValue = "1") int pageNum) {
        PageInfo pageInfo = orderService.queryByPage(pageNum, condition);
        List<Order> list = pageInfo.getList();
        //数据model封装翻页数据
        mv.addObject("list", list);
        //数据model封装翻页信息
        mv.addObject("page", pageInfo);
        mv.setViewName("admin/order/orderList");
        return mv;
    }


    /**
     * orderSearch  处理器
     *
     * @param order
     * @return
     */
    @RequestMapping("/orderSearch")
    public String orderSearch(Order order) {
        condition = order;
        //重定向到OrderList,不经过视图解析器
        return "redirect:/order/orderList";
    }


    /**
     * orderDel 处理器
     *
     * @param mv
     * @param orderId
     * @return
     */
    @RequestMapping("/orderDel")
    public ModelAndView orderDel(ModelAndView mv, String orderId) {
        boolean rs = orderService.removeOrder(orderId);
        //删除成功
        if (rs) {
            result.setOk(true);
            result.setInfo("删除成功!");
            //给删除成功的链接
            result.setHref("/order/orderList");

            //删除失败
        } else {
            result.setOk(false);
            result.setInfo("删除失败!");
            //给删除失败的链接
            result.setHref("javascript:history.back(-1)");
        }
        //封装数据模型
        mv.addObject("result", result);
        //封装视图
        mv.setViewName("admin/message");
        return mv;
    }


    /**
     * orderAdd 处理器
     *
     * @param mv
     * @param order
     * @return
     */
    @RequestMapping("/orderAdd")
    public ModelAndView orderAdd(ModelAndView mv, Order order) {
        boolean flag = orderService.addOrder(order);
        //添加成功
        if (flag) {
            result.setOk(true);
            result.setInfo("添加成功!");
            result.setHref("/order/orderList");

            //添加失败
        } else {
            result.setOk(false);
            result.setInfo("添加失败!");
            result.setHref("javascript:history.back(-1)");
        }
        //封装数据模型
        mv.addObject("result", result);
        //封装视图
        mv.setViewName("admin/message");
        return mv;
    }


    /**
     * checkOrderId  处理器
     *
     * @param orderId
     * @return
     * @ResponseBody 加注释响应体:设置回显数据为string型,不经过视图解析器
     */
    @RequestMapping("/checkOrderId")
    @ResponseBody
    public String checkOrderId(String orderId) {
        Order order = orderService.queryOrder(orderId);
        //占用
        if (order != null) {
            return "订单编号已经存在！";
            //未占用
        } else {
            return "";
        }
    }


    /**
     * 根据orderId查询
     * orderInfo 处理器
     *
     * @param mv
     * @param orderId
     * @return
     */
    @RequestMapping("orderInfo")
    public ModelAndView orderInfo(ModelAndView mv, String orderId) {
        Order order = orderService.queryOrder(orderId);
        //数据model封装翻页数据
        mv.addObject("order", order);
        mv.setViewName("admin/order/orderEdit");
        return mv;
    }


    /**
     * orderEdit 处理器
     *
     * @param mv
     * @param order
     * @return
     */
    @RequestMapping("/orderEdit")
    public ModelAndView orderEdit(ModelAndView mv, Order order) {
        boolean flag = orderService.modifyOrder(order);
        //修改成功
        if (flag) {
            result.setOk(true);
            result.setInfo("修改成功!");
            result.setHref("/order/orderList");

            //添加失败
        } else {
            result.setOk(false);
            result.setInfo("修改失败!");
            result.setHref("javascript:history.back(-1)");
        }
        //封装数据模型
        mv.addObject("result", result);
        //封装视图
        mv.setViewName("admin/message");
        return mv;
    }


}
